(function($){
    function initSetState() {
        $('.but_set_state').on('click', function(){
            var obj = $(this);
            var state = obj.attr('dstate');
            var id = obj.attr('did');
            var url = obj.attr('durl');
            var info = '删除';
            switch(state) {
                case '-1':
                    info = '删除';
                    break;
                case '0':
                    info = '关闭';
                    break;
                case '1':
                    info = '开通';
                    break;
            }
            layer.confirm(
                '确定要【' + info + '】此城市吗？',
                {
                    icon: 3,
                    title: '系统提示',
                    btn: ['确 定', '取 消']
                },
                function(index){
                    layer.close(index);
                    layer.load();
                    $.post(
                        url,
                        {id: id, state: state},
                        function(data){
                            if(data.status == 10000) {
                                location.reload(true);
                            } else {
                                layer.closeAll();
                                layer.alert(data.errmsg);
                            }
                        },
                        'json'
                    );
                },
                function(index){
                    layer.close(index);
                }
            );
        });
    }
    function initSetOrder() {
        $('.but_set_order').on('click', function(){
            var obj = $(this);
            var order = obj.attr('dorder');
            var id = obj.attr('did');
            var url = obj.attr('durl');
            var title = obj.attr('dtitle');
            layer.prompt({
                title: '输入【'+title+'】新的排序值，并确认',
                value: order,
                formType: 0
            }, function(v, index){
                if(!$.isNumeric(v)) {
                    layer.alert("排序值必须是大于等于0的数字！");
                    return;
                }
                layer.close(index);
                layer.load();
                $.post(
                    url,
                    {id: id, order: v},
                    function(data){
                        if(data.status == 10000) {
                            location.reload(true);
                        } else {
                            layer.closeAll();
                            layer.alert(data.errmsg);
                        }
                    },
                    'json'
                );
            });
        });
    }
    function add() {

        var obj = $(this);
        url = obj.attr('durl');
        layer.open({
            type: 2,
            title: '开通新城市',
            shadeClose: false,
            shade: 0.8,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '300px'],
            content: url
        });
    }
    $(function(){
        layer.config({
            extend: 'extend/layer.ext.js'
        });
        initSetState();
        initSetOrder();
        $('.but_add_city').on('click', add);
    });
})(jQuery);